<?php

// +----------------------------------------------------------------------
// | Copyright (c) 2018-2099 http://www.wekwo.com All rights reserved.
// +----------------------------------------------------------------------
// | Author: 天空蓝丶 <wekyun@163.com>
// +----------------------------------------------------------------------
// | Date：2021/03
// +----------------------------------------------------------------------
function dd($data = '', $detailed = 0)
{
    if (is_bool($data)) {
        var_dump($data);
    } else if (is_null($data)) {
        var_dump(null);
    } else {
        if (is_string($data)) {
            echo $data;
        } else {
            if ($detailed) {
                echo '<pre>';
                var_dump($data);
                echo '</pre>';
            } else {
                echo '<pre>';
                print_r($data);
                echo '</pre>';
            }
        }
    }
    exit();
}

//安装程序
//error_reporting(0);
//检查是否已安装
if (file_exists('../install.lock')) {
    $msg = '<h1 style="font-size: 60px;">:(</h1>';
    $msg .= '<p>很抱歉，wekcms程序已安装</p>';
    $msg .= '<p>如需重新安装请删除根目录的/install.lock</p>';
    $msg .= '<p style="font-weight: bold;">若您现在是线上环境,请及时删除install目录及其子目录文件</p>';
    echo $msg;
    exit();
}

//状态
$errmsg = 0;

//相关方法
function check_disable()
{
    $string = ini_get("disable_functions");
    if (strpos($string, 'opendir') !== false) {
        $GLOBALS['errmsg'] = 1;
        return '<b>关闭中！</b>';
    } else {
        return '开启';
    }
}

function check_chinese()
{
    if (preg_match('/[\x{4e00}-\x{9fa5}]/u', $_SERVER['DOCUMENT_ROOT']) > 0) {
        $GLOBALS['errmsg'] = 1;
        return '<b>网站路径中不能含有中文！</b>';
    } else {
        return $_SERVER['DOCUMENT_ROOT'];
    }
}

function check_version()
{

    if (PHP_VERSION < 5.6) {
        $GLOBALS['errmsg'] = 1;
        return '<b>' . PHP_VERSION . '不满足</b>';
    } else {
        return PHP_VERSION;
    }
}

//检查目录是否可写入
function new_is_writeable($file)
{
    if (is_dir($file)) {
        $dir = $file;
        if ($fp = @fopen("$dir/test.txt", 'w')) {
            @fclose($fp);
            @unlink("$dir/test.txt");
            $writeable = 1;
        } else {
            $writeable = 0;
            $GLOBALS['errmsg'] = 1;
        }
    } else {
        if ($fp = @fopen($file, 'a+')) {
            @fclose($fp);
            $writeable = 1;
        } else {
            $writeable = 0;
            $GLOBALS['errmsg'] = 1;
        }
    }

    return $writeable;
}

//获取后台文件名
function get_admin_url()
{
    //读取根目录文件
    $admin_url = '';
    $filepath = '../';
    if (false !== ($handle = opendir($filepath))) {
        $i = 0;
        while (false !== ($file = readdir($handle))) {
            //去掉"“.”、“..”以及带“.xxx”后缀的文件
            if ($file != "." && $file != ".." && strpos($file, ".")) {
                if (strpos($file, '.php') !== false && $file != 'index.php') {
                    $data = file_get_contents('../' . $file);
                    if (strpos($data, "define('ROOT_PATH', dirname(__FILE__) . DIRECTORY_SEPARATOR)") !== false) {
                        $admin_url = $file;
                        break;
                    }
                }
            }
        }
        //关闭句柄
        closedir($handle);
    }
    if ($admin_url == '') {
        exit('缺少后台文件！');
    }
    return $admin_url;
}

//获取域名
function get_domain()
{
    if (!empty($_SERVER['HTTPS']) && strtolower($_SERVER['HTTPS']) !== 'off') {
        $protocol = "https://";
    } elseif (isset($_SERVER['HTTP_X_FORWARDED_PROTO']) && $_SERVER['HTTP_X_FORWARDED_PROTO'] === 'https') {
        $protocol = "https://";
    } elseif (!empty($_SERVER['HTTP_FRONT_END_HTTPS']) && strtolower($_SERVER['HTTP_FRONT_END_HTTPS']) !== 'off') {
        $protocol = "https://";
    } else {
        $protocol = "http://";
    }

    if (isset($_SERVER['HTTP_X_FORWARDED_HOST'])) {
        $host = $_SERVER['HTTP_X_FORWARDED_HOST'];
    } elseif (isset($_SERVER['HTTP_HOST'])) {
        $host = $_SERVER['HTTP_HOST'];
    } else {
        if (isset($_SERVER['SERVER_PORT'])) {
            $port = ':' . $_SERVER['SERVER_PORT'];
            if ((':80' == $port && 'http://' == $protocol) || (':443' == $port && 'https://' == $protocol)) {
                $port = '';
            }
        } else {
            $port = '';
        }
        if (isset($_SERVER['SERVER_NAME'])) {
            $host = $_SERVER['SERVER_NAME'] . $port;
        } else if (isset($_SERVER['SERVER_ADDR'])) {
            $host = $_SERVER['SERVER_ADDR'] . $port;
        }
    }
    return $protocol . $host;
}

/**
 * 解析SQL文件为SQL语句数组
 * @param string $path
 * @return array|mixed|string
 */
function parseSQL($path = '')
{
    $sql = file_get_contents($path);
    //替换掉15个字符串
    $sql = substr($sql, 14);
    $sql = explode("\r\n", $sql);
    //先消除--注释
    $sql = array_filter($sql, function ($data) {
        if (empty($data) || preg_match('/^--.*/', $data)) {
            return false;
        } else {
            return true;
        }
    });
    $sql = implode('', $sql);
    //删除/**/注释
    $sql = preg_replace('/\/\*.*\*\//', '', $sql);
    return $sql;


}

//检查安装进度
$act = isset($_GET['act']) ? $_GET['act'] : '';
switch ($act) {
    case 'step1':
        $tpl = include('tpl/install1.html');
        break;
    case 'step2':
        //检测是否有备份数据库
        //读取备份数据库
        $dir = '../cache/backups';
        $fileArray = array();
        if (false != ($handle = opendir($dir))) {
            $fileArray[0] = '备份数据列表';
            $i = 1;
            while (false !== ($file = readdir($handle))) {
                //去掉"“.”、“..”以及带“.xxx”后缀的文件
                if ($file != "." && $file != ".." && (strpos($file, ".php") !== false) && (strpos($file, '_v') === false)) {
                    $fileArray[$i] = $file;
                    $i++;
                }
            }
            closedir($handle);//关闭资源句柄
        }
        $dblists = $fileArray;
        $admin_url = get_admin_url();
        $tpl = include('tpl/install2.html');
        break;
    case 'step3':
        try {
            $db = new PDO("mysql:host=" . $_POST['host'] . ";port=" . $_POST['port'], $_POST['user'], $_POST['password']);
            $newtable = "CREATE DATABASE IF NOT EXISTS `" . $_POST['name'] . "` DEFAULT CHARACTER SET utf8;";
            $resdb = $db->exec($newtable);
            $db->query("set names utf8");
            if (!$resdb) {
                echo '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /></head><body><script type=text/javascript>alert("您没有创建数据库权限，请手动填写数据库！");javascript:history.go(-1);</script></body></html>';
                exit;
            }
            $pdo = new PDO("mysql:host=" . $_POST['host'] . ";port=" . $_POST['port'] . ";dbname=" . $_POST['name'], $_POST['user'], $_POST['password']);

            //更新database.php
            $config['db']['type'] = 'mysql';
            $config['db']['hostname'] = $_POST['host'];
            $config['db']['database'] = $_POST['name'];
            $config['db']['username'] = $_POST['user'];
            $config['db']['password'] = $_POST['password'];
            $config['db']['hostport'] = $_POST['port'];
            $config['db']['charset'] = 'utf8';
            $config['db']['prefix'] = $_POST['prefix'];

            $config['redis'] = array(
                'SAVE_HANDLE' => 'Redis',
                'HOST' => '127.0.0.1',
                'PORT' => 6379,
                'AUTH' => null,
                'TIMEOUT' => 0,
                'RESERVED' => null,
                'RETRY_INTERVAL' => 100,
                'RECONNECT' => false,
                'EXPIRE' => 1800
            );

            $ress = file_put_contents('../configs/database.php', '<?php /*数据库配置*/ return ' . var_export($config, true) . '; ?>');
        } catch (PDOException $e) {
            echo '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /></head><body><script type=text/javascript>alert("数据库连接失败！");javascript:history.go(-1);</script></body></html>';
        }
        $db = $_POST['go_backup'] == 1 ? $_POST['backup_db'] : '';//判断是否是备份安装

        $data = "<?php
        /**
         * User: 天空蓝丶
         */
		!defined('ROUTE_TYPE') and define('ROUTE_TYPE', 0); // 1：pathinfo模式（自定义路由，需要伪静态） 0：兼容模式，?m=index&c=index&a=init
        !defined('DEBUG') and define('DEBUG', 1); // 1: 开发模式， 2: 线上调试：日志记录，0：线上: 关闭调试日志
        !defined('ERR') and define('ERR', 'on'); // 1: on 开启系统报错，报错更精准， off采用优化的报错
        require __DIR__ . '/vendor/autoload.php';
        define('ROOT_PATH', dirname(__FILE__) . DIRECTORY_SEPARATOR);//项目根目录
        include ROOT_PATH . '/wekcms/app.php';//引入框架初始化文件
        base::run();//执行初始化方法
				
				";
        $admin_url = get_admin_url();
        //传入管理员信息
        $admin_name = $_POST['admin_name'];
        $admin_pass = $_POST['admin_pass'];
        $tpl = include('tpl/install3.html');
        break;
    case 'step4':
        $tpl = include('tpl/install4.html');
        break;
    case 'step5':
        $admin_url = get_domain() . '/' . get_admin_url();
        //生成安装完成的标识文件
        $res = file_put_contents('../install.lock', '安装检测文件，请不要删除此文件，否则会触发重新安装程序');
        $tpl = include('tpl/install5.html');
        break;
    case 'install_testdb':
        $start = ((int)$_POST['start'] == 0) ? 1 : $_POST['start'];
        $to = ((int)$_POST['to'] == 0) ? 1 : $_POST['to'];
        $config = include('../Conf/config.php');
        $db = new PDO("mysql:host=" . $config['db']['host'] . ";port=" . $config['db']['port'] . ";dbname=" . $config['db']['dbname'], $config['db']['username'], $config['db']['password']);
        $sql = file_get_contents('test.php');
        $sql = str_replace('jz_', $config['db']['prefix'], $sql);
        $count = 100;
        $sql = substr($sql, 14);
        $sql .= "UPDATE `jz_level` SET `name`='" . $_POST['admin_name'] . "',`pass`='" . md5(md5($_POST['admin_pass']) . 'YF') . "' , `regtime` = '" . time() . "' , `logintime` = " . time() . "   WHERE id=1";
        $db->query("set names utf8");
        $db->exec($sql);
        echo json_encode(array('count' => $count, "start" => 0, "to" => $count));
        exit;
        break;
    case 'go_install':
        $start = ((int)$_POST['start'] == 0) ? 1 : $_POST['start'];
        $to = ((int)$_POST['to'] == 0) ? 1 : $_POST['to'];
        $config = include('../configs/database.php');
        if ($_GET['db'] == '') {//非备份安装
            $sql = file_get_contents('db.sql');
            $pwd = md5(md5($_POST['admin_pass']) . $_POST['admin_name']);

            $sql .= "INSERT INTO `wek_admin` (`admin_account`, `pwd`, `root_level`, `add_time`) VALUES ('{$_POST['admin_name']}', '{$pwd}', -1, '2021-02-05 00:00:00');";
//            $sql .= "UPDATE `wek_admin` SET `admin_account`='" . $_POST['admin_name'] . "',`pwd`='" . md5(md5($_POST['admin_pass']) . $_POST['admin_name']) . "' , `root_level` = '" . -1 . "' , `add_time` = " . time() . "   WHERE id=1";
            $sql = substr($sql, 7);//截取前面的
            $sql = str_replace('wek_', $config['db']['prefix'], $sql);//把前缀改成用户自定义的前缀

            $count = 100;
            $db = new PDO("mysql:host=" . $config['db']['hostname'] . ";port=" . $config['db']['hostport'] . ";dbname=" . $config['db']['database'], $config['db']['username'], $config['db']['password']);
            $db->query("set names utf8");
            $r = $db->exec($sql);

            echo json_encode(array('count' => $count, "start" => 0, "to" => $count, 'code' => 0));
            exit;
        } else {
            dd(333);
            $db = new PDO("mysql:host=" . $config['db']['host'] . ";port=" . $config['db']['port'] . ";dbname=" . $config['db']['dbname'], $config['db']['username'], $config['db']['password']);

            $db->query("set names utf8");
            //$sql = file_get_contents('../cache/backups/'.$_GET['db']);
            $path = $_GET['db'];
            $filename_arr = explode('.php', $path);
            $filename = $filename_arr[0];

            //读取备份数据库
            $dir = '../cache/backups';
            $fileArray = array();
            $fileArray[] = $dir . '/' . $filename . '.php';
            for ($i = 1; file_exists($dir . '/' . $filename . '_v' . $i . '.php') === true; $i++) {
                $fileArray[] = $dir . '/' . $filename . '_v' . $i . '.php';
            }

            foreach ($fileArray as $path) {
                $sql = parseSQL($path);
                try {
                    $n = $db->exec($sql);
                    if (!$n) {
                        $msg = $db->errorInfo();
                        if ($msg[2]) {
                            echo json_encode(array('code' => 1, 'msg' => '数据库错误：' . $msg[2] . end($sql)));
                            exit;
                        }
                    }


                } catch (PDOException $e) {
                    echo json_encode(array('code' => 1, 'msg' => $e->getMessage()));
                    exit;
                }

            }

            echo json_encode(array('count' => 100, "start" => 0, "to" => 100, 'code' => 0));
            exit;

        }
        dd(99);

        break;
    case 'testdb':
        try {
            //$_opts_values = array(PDO::ATTR_PERSISTENT=>true,PDO::ATTR_ERRMODE=>2,PDO::MYSQL_ATTR_INIT_COMMAND=>'SET NAMES utf8');
            //$db = new PDO("mysql:host=".$_POST['host'].";port=".$_POST['port'].";dbname=".$_POST['name'],$_POST['user'], $_POST['password'],$_opts_values);
            $db = new PDO("mysql:host=" . $_POST['host'] . ";port=" . $_POST['port'], $_POST['user'], $_POST['password']);
            echo json_encode(['code' => 0, 'msg' => 'success']);
            exit;
        } catch (PDOException $e) {
            echo json_encode(['code' => 1, 'msg' => '数据库连接失败，请检查数据库配置！']);
            exit;
        }

        break;
    default:
        //协议
        $tpl = include('tpl/index.html');
        break;
}















